﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WD.Db.Contract;
using WD.Db.Contract.Model;
using WD.Framework.Contract;
using WD.Web.MVC.Common;
using WD.Web.MVC.Controllers.Filters;

namespace WD.Web.MVC.Areas.BaseArea.Controllers
{
    public class Rbac_FunctionModelController : AdminControllerBase
    {
        //
        // GET: /BaseArea/Rbac_FunctionModel/

        [AdminOnlineFilter]
        public ActionResult Index(RbacFunctionModelRequest request)
        {

            var list1 = Base_CodeDataService.GetBase_CodeDataListByType(CommonCode.功能模块级别).ToList();
            ViewBag.ModelLevel = new SelectList(list1, "ID", "CodeName");

            var list2 = Base_CodeDataService.GetBase_CodeDataListByType(CommonCode.功能模块类型).ToList();
            ViewBag.ModelType = new SelectList(list2, "ID", "CodeName");


            var result = Rbac_FunctionModelService.GetRbac_FunctionModelList(request);
            return View(result);
        }

        [AdminOnlineFilter]
        public ActionResult Create()
        {
            var request = new RbacFunctionModelRequest();
            // request.ParentId = "";
            request.ModelLevelList = new List<int> { CommonCode.功能模块级别_一级模块, CommonCode.功能模块级别_二级模块 };
            request.GetAll = true;
            var list = Rbac_FunctionModelService.GetRbac_FunctionModelList(request).OrderBy(x => x.ModelCode).ToList();
            foreach (var item in list)
            {
                var extLength = item.ModelCode.Split('_').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　　";
                }
                item.ModelName = tmp + item.ModelName;
            }
            var selectListFunModel = new SelectList(list, "ModelID", "ModelName");
            ViewBag.selectListFunModel = selectListFunModel;


            var list2 = Rbac_FunctionModelService.GetRbac_Base_CodeDataListByPid(CommonCode.功能模块级别).OrderBy(x => x.AllId).ToList();
            foreach (var item in list2)
            {
                var extLength = item.AllId.Split(',').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　　";
                }
                item.CodeName = tmp + item.CodeName;
            }
            var selectListGnJb = new SelectList(list2, "CodeID", "CodeName");
            ViewBag.selectListGnJb = selectListGnJb;

            list2 = Rbac_FunctionModelService.GetRbac_Base_CodeDataListByPid(CommonCode.功能模块类型).OrderBy(x => x.AllId).ToList();
            foreach (var item in list2)
            {
                var extLength = item.AllId.Split(',').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　　";
                }
                item.CodeName = tmp + item.CodeName;
            }
            var selectListGnLx = new SelectList(list2, "CodeID", "CodeName");
            ViewBag.selectListGnLx = selectListGnLx;

            //            var list3 = Rbac_FunctionModelService.GetRbac_Base_CodeDataListByPid(CommonCode.所属功能模块).OrderBy(x => x.AllId).ToList();
            //            foreach (var item in list3)
            //            {
            //                var extLength = item.AllId.Split(',').Count();
            //                var tmp = "";
            //                for (int i = 1; i < extLength; i++)
            //                {
            //                    tmp += "　　";
            //                }
            //                item.CodeName = tmp + item.CodeName;
            //            }
            //            var selectListCat = new SelectList(list3, "CodeID", "CodeName");
            //            ViewBag.selectListCat = selectListCat;

            var model = new Rbac_FunctionModel();
            return View(model);
        }


        [HttpPost]
        public string Create(FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = new Rbac_FunctionModel();
            TryUpdateModel(model);
            try
            {
                Rbac_FunctionModelService.SaveRbac_FunctionModel(model, LoginInfo.LoginId);

                #region 新增增删查改功能权限

                model = Rbac_FunctionModelService.GetRbac_FunctionModelByUrl(model.Url);
                if (model != null)
                {
                    var rootUrl = model.Url.Replace("/Index", "");
                    var rootName = model.ModelName.Replace("列表", "");
                    var add = Request.Form["checkbox-inl-mutil"];
                    if (add == "on")
                    {
                        var modelAdd = new Rbac_FunctionModel
                            {
                                Url = rootUrl + "/Create",
                                ModelName = "新增" + rootName,
                                ModelLevel = CommonCode.功能模块级别_三级模块,
                                ModelType = CommonCode.功能模块类型_系统模块,
                                ParentID = model.ModelID,
                                Icon = null
                            };
                        Rbac_FunctionModelService.SaveRbac_FunctionModel(modelAdd, LoginInfo.LoginId);

                        modelAdd = new Rbac_FunctionModel
                            {
                                Url = rootUrl + "/Edit",
                                ModelName = "查看" + rootName,
                                ModelLevel = CommonCode.功能模块级别_三级模块,
                                ModelType = CommonCode.功能模块类型_系统模块,
                                ParentID = model.ModelID,
                                Icon = null
                            };
                        Rbac_FunctionModelService.SaveRbac_FunctionModel(modelAdd, LoginInfo.LoginId);

                        modelAdd = new Rbac_FunctionModel
                            {
                                Url = rootUrl + "/Edit_Save",
                                ModelName = "修改" + rootName,
                                ModelLevel = CommonCode.功能模块级别_三级模块,
                                ModelType = CommonCode.功能模块类型_系统模块,
                                ParentID = model.ModelID,
                                Icon = null
                            };
                        Rbac_FunctionModelService.SaveRbac_FunctionModel(modelAdd, LoginInfo.LoginId);

                        modelAdd = new Rbac_FunctionModel
                            {
                                Url = rootUrl + "/Deleted",
                                ModelName = "删除" + rootName,
                                ModelLevel = CommonCode.功能模块级别_三级模块,
                                ModelType = CommonCode.功能模块类型_系统模块,
                                ParentID = model.ModelID,
                                Icon = null
                            };
                        Rbac_FunctionModelService.SaveRbac_FunctionModel(modelAdd, LoginInfo.LoginId);
                    }
                #endregion

                }
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }
            return ajax.Suc("新增功能模块成功");


        }


        [AdminOnlineFilter(Url = "/BaseArea/Rbac_FunctionModel/Edit")]
        public ActionResult Edit(int id)
        {
            var model = Rbac_FunctionModelService.GetRbac_FunctionModel(id);
            // var model=AccountService

            var request = new RbacFunctionModelRequest();
            // request.ParentId = "";
            request.ModelLevelList = new List<int> { CommonCode.功能模块级别_一级模块, CommonCode.功能模块级别_二级模块 };
            request.GetAll = true;
            var list = Rbac_FunctionModelService.GetRbac_FunctionModelList(request).OrderBy(x => x.ModelCode).ToList();
            foreach (var item in list)
            {
                var extLength = item.ModelCode.Split('_').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　　";
                }
                item.ModelName = tmp + item.ModelName;
            }
            var selectListFunModel = new SelectList(list, "ModelID", "ModelName", model.ParentID);
            ViewBag.selectListFunModel = selectListFunModel;

            var list2 = Rbac_FunctionModelService.GetRbac_Base_CodeDataListByPid(CommonCode.功能模块级别).OrderBy(x => x.AllId).ToList();
            foreach (var item in list2)
            {
                var extLength = item.AllId.Split(',').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　　";
                }
                item.CodeName = tmp + item.CodeName;
            }
            var selectListGnJb = new SelectList(list2, "CodeID", "CodeName", model.ModelLevel);
            ViewBag.selectListGnJb = selectListGnJb;

            list2 = Rbac_FunctionModelService.GetRbac_Base_CodeDataListByPid(CommonCode.功能模块类型).OrderBy(x => x.AllId).ToList();
            foreach (var item in list2)
            {
                var extLength = item.AllId.Split(',').Count();
                var tmp = "";
                for (int i = 1; i < extLength; i++)
                {
                    tmp += "　　";
                }
                item.CodeName = tmp + item.CodeName;
            }
            var selectListGnLx = new SelectList(list2, "CodeID", "CodeName", model.ModelType);
            ViewBag.selectListGnLx = selectListGnLx;

            //            var list3 = Rbac_FunctionModelService.GetRbac_Base_CodeDataListByPid(CommonCode.所属功能模块).OrderBy(x => x.AllId).ToList();
            //            foreach (var item in list3)
            //            {
            //                var extLength = item.AllId.Split(',').Count();
            //                var tmp = "";
            //                for (int i = 1; i < extLength; i++)
            //                {
            //                    tmp += "　　";
            //                }
            //                item.CodeName = tmp + item.CodeName;
            //            }
            //            var selectListCat = new SelectList(list3, "CodeID", "CodeName");
            //            ViewBag.selectListCat = selectListCat;

            //var model = new Rbac_FunctionModel();
            return View(model);
        }

        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_FunctionModel/Edit_Save")]
        public string Edit(int id, FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = Rbac_FunctionModelService.GetRbac_FunctionModel(id);
            TryUpdateModel(model);
            try
            {
                //捕捉  throw new BusinessException("ParentID", "父级无法选择自己！");
                Rbac_FunctionModelService.SaveRbac_FunctionModel(model, LoginInfo.LoginId);
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
                throw;
            }

            return ajax.Suc("信息修改成功!");
        }


        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_FunctionModel/Deleted")]
        public string Deleted(List<int> ids)
        {
            var ajax = new AjaxResult();

            try
            {
                Rbac_FunctionModelService.DeleteRbac_FunctionModel(ids, LoginInfo.LoginId);
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }

            return ajax.Suc("信息已成功删除!");
        }

    }
}
